//
//  FLMManageOrderListController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/16.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMManageOrderListController.h"
#import "FLMManageOrderListCell.h"
#import "FLMOrderSearchView.h"
#import "FLMOrderDetailController.h"

#import "FLMOrderModel.h"
#import "SPBOrderDelegate.h"

@interface FLMManageOrderListController ()<UITableViewDataSource,UITableViewDelegate>
{
    orderType orderT;
    NSInteger pages;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) FLMOrderSearchView *searchView;
/** 记录订单模型数组 */
@property (nonatomic,strong)NSMutableArray *orderMessageArray;

@property (nonatomic,strong)SPBOrderDelegate *dataDelegate;


@end

@implementation FLMManageOrderListController

- (void)viewDidLoad {
    [super viewDidLoad];
    pages = 1;
    _dataDelegate = [[SPBOrderDelegate alloc] init];
    if ([_orderTypeName isEqualToString:@"待审核订单"]) {
        orderT = orderTypeWaitCheck;
    }
    else if ([_orderTypeName isEqualToString:@"淘宝订单"]){
        orderT = orderTypeTao;
    }
    else{
        orderT = orderTypeMarket;
    }
    [self layoutUI];
    [self setUpTaoBaoOrderRefresh];

}

- (void)setOrderTypeName:(NSString *)orderTypeName
{
    _orderTypeName = orderTypeName;
    if ([_orderTypeName isEqualToString:@"待审核订单"]) {
        orderT = orderTypeWaitCheck;
    }
    else if ([_orderTypeName isEqualToString:@"淘宝订单"]){
        orderT = orderTypeTao;
    }
    else{
        orderT = orderTypeMarket;
    }

}

- (NSMutableArray *)orderMessageArray {
    if (_orderMessageArray == nil) {
        _orderMessageArray = [NSMutableArray array];
    }
    return _orderMessageArray;
}
- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}


- (void)layoutUI
{
    if (!_searchView) {
        _searchView = [[FLMOrderSearchView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 35)];
        [self.view addSubview:_searchView];
        __weak typeof (self)weakSelf = self;
        [_searchView setSearchBlock:^(UITextField *textField) {
            if (!textField.text.length) {
                [SVProgressHUD showErrorWithStatus:@"搜索订单号不能为空"];
                return ;
            }
            [weakSelf searchData];
        }];
    }
    
    self.dataArr = [@[@"1",@"1"] mutableCopy];
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_searchView.frame), ScreenW, ScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[FLMManageOrderListCell class] forCellReuseIdentifier:@"FLMManageOrderListCell"];
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            _tableView.layoutMargins = UIEdgeInsetsZero;
        }

        [self.view addSubview:_tableView];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //刷新控件
}


#pragma mark --------------------
#pragma mark 刷新控件
- (void)setUpTaoBaoOrderRefresh
{
    //header
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    //footer
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}



#pragma mark --UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderMessageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FLMManageOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMManageOrderListCell"];
    FLMOrdersModel *orderTaoBaoModel = self.orderMessageArray[indexPath.row];
    [cell configCellWithModel:orderTaoBaoModel];
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

#pragma mark --------------------
#pragma mark 跳转订单详情控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    FLMOrderDetailController *detailVC = [[FLMOrderDetailController alloc]init];
    
    detailVC.model = self.orderMessageArray[indexPath.row];
    
    detailVC.title = [NSString stringWithFormat:@"%@详情",self.orderTypeName];
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark --私有方法
#pragma mark 搜索
- (void)searchData
{
    
}

/**
 *  刷新数据
 */

- (void)refreshData
{

    //@"待审核订单",@"淘宝订单",@"商城订单"
    [self.dataDelegate datasWithRequestType:orderT WithBlock:^(BOOL isSuccess, NSMutableArray *datas) {
        if (isSuccess) {
            [self.orderMessageArray removeAllObjects];
            self.orderMessageArray = datas;
            [self.tableView reloadData];
            pages = 0;
        }
        [self.tableView.mj_header endRefreshing];
    }];
}


/**
 *  加载数据
 */
- (void)loadMoreData
{
    [self.tableView.mj_footer endRefreshing];
    
    [self.dataDelegate datasWithRequestType:orderT pages:pages WithBlock:^(BOOL isSuccess, NSMutableArray *datas) {
        if (isSuccess) {
            [self.orderMessageArray addObjectsFromArray: datas];
            [self.tableView reloadData];
            pages++;
        }
        [self.tableView.mj_header endRefreshing];
    }];

}

@end
